package com.dbf.site.client.message.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dbf.site.client.message.model.MessageModel;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.mvp4g.client.view.ReverseViewInterface;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.Style;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.data.shared.loader.BeforeLoadEvent;
import com.sencha.gxt.data.shared.loader.BeforeLoadEvent.BeforeLoadHandler;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.RefreshEvent;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;


public class MessageView  implements IMessageView, ReverseViewInterface<IMessageView.IMessagePresenter>{
	
	private static MessageModelProperties properties = GWT.create(MessageModelProperties.class);
	private IMessageView.IMessagePresenter presenter;
	private FramedPanel container;
	
	private ListStore<MessageModel> store;
	private Grid<MessageModel> grid;
	private IdentityValueProvider<MessageModel> identity;
	private CheckBoxSelectionModel<MessageModel> sm;
	private PagingToolBar toolBar;
	private CheckBox warnLoad;
	private PagingLoader<PagingLoadConfig, PagingLoadResult<MessageModel>> loader; 	
	
	public MessageView(){
		createView();
		
	}
	private void createGrid() {
	 
//	      loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, MessageModel, PagingLoadResult<MessageModel>>(store));
	 
	      IdentityValueProvider<MessageModel> identity = new IdentityValueProvider<MessageModel>();
	      final CheckBoxSelectionModel<MessageModel> selectionModel = new CheckBoxSelectionModel<MessageModel>(identity) {
	        @Override
	        protected void onRefresh(RefreshEvent event) {
	          // this code selects all rows when paging if the header checkbox is selected
	          if (isSelectAllChecked()) {
	            selectAll();
	          }
	          super.onRefresh(event);
	        }
	      };
	 
	      ColumnConfig<MessageModel, String> forumColumn = new ColumnConfig<MessageModel, String>(properties.messageComment(), 150, "Commentaire");
	      ColumnConfig<MessageModel, Date> dateColumn = new ColumnConfig<MessageModel, Date>(properties.messageCreationDate(), 150, "Date");
	 
	      dateColumn.setCell(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
	 
	      List<ColumnConfig<MessageModel, ?>> columns = new ArrayList<ColumnConfig<MessageModel, ?>>();
	      // The selection model provides the first column config
	      columns.add(selectionModel.getColumn());
	      columns.add(forumColumn);
	      columns.add(dateColumn);
	 
	      ColumnModel<MessageModel> cm = new ColumnModel<MessageModel>(columns);
	 
	      grid = new Grid<MessageModel>(store, cm) ;
//	    		  {
//	        @Override
//	        protected void onAfterFirstAttach() {
//	          super.onAfterFirstAttach();
//	          Scheduler.get().scheduleDeferred(new ScheduledCommand() {
//	            @Override
//	            public void execute() {
//	              loader.load();
//	            }
//	          });
//	        }
//	      };
	      grid.setSelectionModel(selectionModel);
	      grid.getView().setForceFit(true);
	      grid.setLoadMask(true);
//	      grid.setLoader(loader);
	      grid.setColumnReordering(true);
	 
	      warnLoad = new CheckBox();
	      warnLoad.setBoxLabel("Warn before loading new data");
	      warnLoad.setValue(false);
	 
//	      toolBar = new PagingToolBar(50);
//	      toolBar.getElement().getStyle().setProperty("borderBottom", "none");
//	      toolBar.bind(loader);
	      container.add(grid);
//	      container.add(toolBar);
//	      container.add(warnLoad);
	 
	      // If the warn checkbox is active, then present a warning and stop any load
	      // after the first if the user clicks cancel
//	      loader.addBeforeLoadHandler(new BeforeLoadHandler<PagingLoadConfig>() {
//	        boolean initialLoad = true;
//	 
//	        @Override
//	        public void onBeforeLoad(BeforeLoadEvent<PagingLoadConfig> event) {
//	          if (!initialLoad && warnLoad.getValue()) {
//	            event.setCancelled(!Window.confirm("Are you sure you want to do that?"));
//	          }
//	          initialLoad = false;
//	        }
//	      });
	  }
	private void createView(){
		container = new FramedPanel();
	    container.setHeight("100%");
	    container.setHeaderVisible(true);
//	    container.setBodyStyle("background-color:#3892d3;");
//	    VerticalLayoutContainer verticalLayoutContainer = new VerticalLayoutContainer();
//	      verticalLayoutContainer.setBorders(true);
//	      verticalLayoutContainer.add(grid, new VerticalLayoutData(1, 1));
//	      verticalLayoutContainer.add(toolBar, new VerticalLayoutData(1, -1));
//	      verticalLayoutContainer.add(warnLoad, new VerticalLayoutData(-1, -1));
	      
		
	}
	
	
	@Override
	public Widget asWidget() {
		return container;
	}

	@Override
	public void setPresenter(IMessagePresenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public IMessagePresenter getPresenter() {
		return this.presenter;
	}
	
	public interface MessageModelProperties extends PropertyAccess<MessageModel> {
		ModelKeyProvider<MessageModel> id();

		ValueProvider<MessageModel, Date> messageCreationDate();

		ValueProvider<MessageModel, String> messageComment();

	}

	@Override
	public void showList(ListStore<MessageModel> store) {
		this.store = store;
		createGrid();
		
	}

}
